﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using AspNetCs.Model;
using AspNetCs.Common;
using AspNetCs.Config;
using System.Data;
namespace AspNetCs.SQLServerDAL
{
    public class Vote_DAL : IVote
    {


        #region IVote 成员

        public Vote_Model GetModel(System.Data.DataRow dr)
        {
            Vote_Model model = new Vote_Model();
            try
            {
                if (dr["ID"] != null) { model.Id = StringHelper.isNumber_int(dr["id"].ToString()); }

                if (dr["Title"] != null) { model.Title = dr["Title"].ToString(); }



                if (dr["Select1"] != null) { model.Select1 = dr["Select1"].ToString(); }
                if (dr["SumNum1"] != null) { model.SumNum1 = StringHelper.isNumber_int(dr["SumNum1"].ToString()); }



                if (dr["Select2"] != null) { model.Select2 = dr["Select2"].ToString(); }
                if (dr["SumNum2"] != null) { model.SumNum2 = StringHelper.isNumber_int(dr["SumNum2"].ToString()); }




                if (dr["Select3"] != null) { model.Select3 = dr["Select3"].ToString(); }
                if (dr["SumNum3"] != null) { model.SumNum3 = StringHelper.isNumber_int(dr["SumNum3"].ToString()); }

                if (dr["Select4"] != null) { model.Select4 = dr["Select4"].ToString(); }
                if (dr["SumNum4"] != null) { model.SumNum4 = StringHelper.isNumber_int(dr["SumNum4"].ToString()); }


                if (dr["Select5"] != null) { model.Select5 = dr["Select5"].ToString(); }
                if (dr["SumNum5"] != null) { model.SumNum5 = StringHelper.isNumber_int(dr["SumNum5"].ToString()); }


                if (dr["Select6"] != null) { model.Select6 = dr["Select6"].ToString(); }
                if (dr["SumNum6"] != null) { model.SumNum6 = StringHelper.isNumber_int(dr["SumNum6"].ToString()); }


                if (dr["Select7"] != null) { model.Select7 = dr["Select7"].ToString(); }
                if (dr["SumNum7"] != null) { model.SumNum7 = StringHelper.isNumber_int(dr["SumNum7"].ToString()); }


                if (dr["Select8"] != null) { model.Select8 = dr["Select8"].ToString(); }
                if (dr["SumNum8"] != null) { model.SumNum8 = StringHelper.isNumber_int(dr["SumNum8"].ToString()); }

                if (dr["Addtime"] != null) { model.Addtime = Convert.ToDateTime(dr["Addtime"].ToString()); }
                if (dr["isNew"] != null) { model.IsNew = StringHelper.isNumber_int(dr["IsNew"].ToString()); }
                if (dr["VoteType"] != null) { model.VoteType = StringHelper.isNumber_int(dr["VoteType"].ToString()); }

            }
            catch (Exception Ex)
            {
                new CustomException(Ex);
            }

            return model;

        }

        public Vote_Model GetModel(int id)
        {
            db.ParameterArry[] par = new db.ParameterArry[1];
            par[0].dbType = SqlDbType.Int;
            par[0].objValue = id;
            par[0].parName = "@id";
            db Conn = new db();
            Vote_Model model = new Vote_Model();
            try
            {
                DataTable dt = Conn.GetSql_DataTable("Select Top 1 * from Vote Where id=@id", par);
                foreach (DataRow dr in dt.Rows)
                {
                    model = GetModel(dr);
                }
            }
            catch (Exception Ex)
            {
                new CustomException(Ex);
            }
            finally
            {
                Conn.close();
            }
            return model;

        }

        public int Add(Vote_Model model)
        {
            db Conn = new db();
            int result = -1;
            try
            {
                #region 参数

                db.ParameterArry[] par = new db.ParameterArry[20];
                par[0].dbType = SqlDbType.Text;
                par[0].parName = "@title";
                par[0].objValue = model.Title;


                par[1].dbType = SqlDbType.VarChar;
                par[1].parName = "@Select1";
                par[1].objValue = model.Select1;

                par[2].dbType = SqlDbType.VarChar;
                par[2].parName = "@Select2";
                par[2].objValue = model.Select2;

                par[3].dbType = SqlDbType.VarChar;
                par[3].parName = "@Select3";
                par[3].objValue = model.Select3;

                par[4].dbType = SqlDbType.VarChar;
                par[4].parName = "@Select4";
                par[4].objValue = model.Select4;

                par[5].dbType = SqlDbType.VarChar;
                par[5].parName = "@Select5";
                par[5].objValue = model.Select5;

                par[6].dbType = SqlDbType.VarChar;
                par[6].parName = "@Select6";
                par[6].objValue = model.Select6;

                par[7].dbType = SqlDbType.VarChar;
                par[7].parName = "@Select7";
                par[7].objValue = model.Select7;

                par[8].dbType = SqlDbType.VarChar;
                par[8].parName = "@Select8";
                par[8].objValue = model.Select8;



                par[9].dbType = SqlDbType.BigInt;
                par[9].parName = "@SumNum1";
                par[9].objValue = model.SumNum1;



                par[10].dbType = SqlDbType.BigInt;
                par[10].parName = "@SumNum2";
                par[10].objValue = model.SumNum2;



                par[11].dbType = SqlDbType.BigInt;
                par[11].parName = "@SumNum3";
                par[11].objValue = model.SumNum3;



                par[12].dbType = SqlDbType.BigInt;
                par[12].parName = "@SumNum4";
                par[12].objValue = model.SumNum4;

                par[13].dbType = SqlDbType.BigInt;
                par[13].parName = "@SumNum5";
                par[13].objValue = model.SumNum5;

                par[14].dbType = SqlDbType.BigInt;
                par[14].parName = "@SumNum6";
                par[14].objValue = model.SumNum6;

                par[15].dbType = SqlDbType.BigInt;
                par[15].parName = "@SumNum7";
                par[15].objValue = model.SumNum7;

                par[16].dbType = SqlDbType.BigInt;
                par[16].parName = "@SumNum8";
                par[16].objValue = model.SumNum8;


                par[17].dbType = SqlDbType.Int;
                par[17].parName = "@isNew";
                par[17].objValue = model.IsNew;


                par[18].dbType = SqlDbType.DateTime;
                par[18].parName = "@Addtime";
                par[18].objValue = model.Addtime;

                par[19].dbType = SqlDbType.Int;
                par[19].parName = "@VoteType";
                par[19].objValue = model.VoteType;



                #endregion

                result = Conn.ExcSql("Insert into Vote(Title,Select1,SumNum1,Select2,SumNum2,Select3,SumNum3,Select4,SumNum4,Select5,SumNum5,Select6,SumNum6,Select7,SumNum7,Select8,SumNum8,isNew,Addtime,VoteType) values(@title,@Select1,@SumNum1,@Select2,@SumNum2,@Select3,@SumNum3,@Select4,@SumNum4,@Select5,@SumNum5,@Select6,@SumNum6,@Select7,@SumNum7,@Select8,@SumNum8,@isNew,@Addtime,@VoteType)", par);

            }
            catch (Exception ex)
            {
                new CustomException(ex);
            }
            finally
            {
                Conn.close();
            }
            return result;
        }

        public int Update(Vote_Model model)
        {
            db Conn = new db();
            int result = -1;
            try
            {
                #region 参数

                db.ParameterArry[] par = new db.ParameterArry[21];
                par[0].dbType = SqlDbType.Text;
                par[0].parName = "@title";
                par[0].objValue = model.Title;


                par[1].dbType = SqlDbType.VarChar;
                par[1].parName = "@Select1";
                par[1].objValue = model.Select1;

                par[2].dbType = SqlDbType.VarChar;
                par[2].parName = "@Select2";
                par[2].objValue = model.Select2;

                par[3].dbType = SqlDbType.VarChar;
                par[3].parName = "@Select3";
                par[3].objValue = model.Select3;

                par[4].dbType = SqlDbType.VarChar;
                par[4].parName = "@Select4";
                par[4].objValue = model.Select4;

                par[5].dbType = SqlDbType.VarChar;
                par[5].parName = "@Select5";
                par[5].objValue = model.Select5;

                par[6].dbType = SqlDbType.VarChar;
                par[6].parName = "@Select6";
                par[6].objValue = model.Select6;

                par[7].dbType = SqlDbType.VarChar;
                par[7].parName = "@Select7";
                par[7].objValue = model.Select7;

                par[8].dbType = SqlDbType.VarChar;
                par[8].parName = "@Select8";
                par[8].objValue = model.Select8;



                par[9].dbType = SqlDbType.BigInt;
                par[9].parName = "@SumNum1";
                par[9].objValue = model.SumNum1;



                par[10].dbType = SqlDbType.BigInt;
                par[10].parName = "@SumNum2";
                par[10].objValue = model.SumNum2;



                par[11].dbType = SqlDbType.BigInt;
                par[11].parName = "@SumNum3";
                par[11].objValue = model.SumNum3;



                par[12].dbType = SqlDbType.BigInt;
                par[12].parName = "@SumNum4";
                par[12].objValue = model.SumNum4;

                par[13].dbType = SqlDbType.BigInt;
                par[13].parName = "@SumNum5";
                par[13].objValue = model.SumNum5;

                par[14].dbType = SqlDbType.BigInt;
                par[14].parName = "@SumNum6";
                par[14].objValue = model.SumNum6;

                par[15].dbType = SqlDbType.BigInt;
                par[15].parName = "@SumNum7";
                par[15].objValue = model.SumNum7;

                par[16].dbType = SqlDbType.BigInt;
                par[16].parName = "@SumNum8";
                par[16].objValue = model.SumNum8;


                par[17].dbType = SqlDbType.Int;
                par[17].parName = "@isNew";
                par[17].objValue = model.IsNew;


                par[18].dbType = SqlDbType.DateTime;
                par[18].parName = "@Addtime";
                par[18].objValue = model.Addtime;

                par[19].dbType = SqlDbType.Int;
                par[19].parName = "@id";
                par[19].objValue = model.Id;

                par[20].dbType = SqlDbType.Int;
                par[20].parName = "@VoteType";
                par[20].objValue = model.VoteType;


                #endregion

                result = Conn.ExcSql("Update  Vote Set Title=@Title,Select1=@Select1,SumNum1=@SumNum1,Select2=@Select2,SumNum2=@SumNum2,Select3=@Select3,SumNum3=@SumNum3,Select4=@Select4,SumNum4=@SumNum4,Select5=@Select5,SumNum5=@SumNum5,Select6=@Select6,SumNum6=@SumNum6,Select7=@Select7,SumNum7=@SumNum7,Select8=@Select8,SumNum8=@SumNum8,isNew=@isNew,Addtime=@Addtime,VoteType=@VoteType where id=@id", par);

            }
            catch (Exception ex)
            {
                new CustomException(ex);
            }
            finally
            {
                Conn.close();
            }
            return result;

        }

        public int Del(int id)
        {
            db.ParameterArry[] par = new db.ParameterArry[1];
            par[0].dbType = SqlDbType.Int;
            par[0].objValue = id;
            par[0].parName = "@id";
            db Conn = new db();
            int result = -1;
            try
            {
                result = Conn.ExcSql("delete  From Vote Where id=@id", par);

            }
            catch (Exception Ex)
            {

                new CustomException(Ex);
            }
            finally
            {
                Conn.close();
            }
            return result;
        }

        public DataTable GetDataTable(string filter)
        {
            DataTable dt = new DataTable("VoteDt");
            string sql = "Select * from Vote where id<>0  " + filter;
            db Conndt = new db();
            try
            {
                dt = Conndt.GetSql_DataTable(sql);
            }
            catch (Exception Ex)
            {
                new CustomException(Ex);
            }
            finally
            {
                Conndt.close();
            }

            return dt;
        }

        #endregion
    }
}
